﻿using DotNetMVC.OA.EFDAL;
using DotNetMVC.OA.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.OA.Test.Controllers
{
    public class DefaultController : Controller
    {
        DataModelContainer db = new DataModelContainer();
        // GET: Default
        public ActionResult Index()
        {
    
            //List<UserInfo> list = db.UserInfo.ToList();
            UserInfoDal dal = new UserInfoDal();
            List<UserInfo> list = dal.GetEntities(u=>u.ID>0).ToList();
            return View(list);
        }
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(string UName)
        {
            UserInfo user = new UserInfo();
            user.UName = UName;
            db.UserInfo.Add(user);
            db.SaveChanges();
            return Redirect("/Default/Index");
        }

        [HttpGet]
        public ActionResult Update(int ID)
        {
            UserInfo user = db.UserInfo.FirstOrDefault(u => u.ID == ID);
            return View(user);
        }
        [HttpPost]
        public ActionResult Update(int ID, string UName)
        {
            UserInfo user = new UserInfo();
            user.ID = ID;
            user.UName = UName;
            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            return Redirect("/Default/Index");
        }

        [HttpGet]
        public ActionResult Delete(int ID)
        {
            UserInfo user = db.UserInfo.FirstOrDefault(u => u.ID == ID);
            db.Entry(user).State = EntityState.Deleted;
            db.SaveChanges();
            return Redirect("/Default/Index");
        }
    }
}